package com.biodiv.community.service;

import com.aliyuncs.exceptions.ClientException;
import com.biodiv.community.client.OssClient;
import com.biodiv.community.constant.RedisKeys;
import com.biodiv.community.manager.cache.RedisCacheManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author: tianquan
 * @date: 2025-06-28  10:14
 * @description: 通用服务
 */
@Service
@RequiredArgsConstructor
public class CommonService {

    private final OssClient aliyunOssClient;
    private final RedisCacheManager redisCacheManager;

    /**
     * 获取阿里云OSS签名
     */
    public OssClient.StsToken getOssSign() {
        OssClient.StsToken stsToken = redisCacheManager.get(RedisKeys.OSS_SIGN, OssClient.StsToken.class);
        if (stsToken == null) {
            try {
                stsToken = aliyunOssClient.generateStsToken();
            } catch (ClientException e) {
                throw new RuntimeException(e);
            }
            redisCacheManager.set(RedisKeys.OSS_SIGN, stsToken, aliyunOssClient.getExpireTime() - 60, TimeUnit.SECONDS);
        }
        return stsToken;
    }

}
